/**
 * Created by aifece on 15/1/18.
 */
(function () {
  $(function(){
    // 调取地址
    function currAddress()
    {
      AddressJs.call(this);
    }
    for (var i in AddressJs.prototype) {
      currAddress.prototype[i] = AddressJs.prototype[i]
    }
    currAddress.prototype.showForm = function()
    {
      var _this = this;
      $(this.option.formDiv).find('input').each(function(){
        _this.deleteMsg($(this));
      });
      $(this.option.formDiv).removeClass('hidden');
      $(this.option.infoDiv).addClass('hidden');
      $(this.option.parentDiv).attr('data-edit-address', 1);
      $('.js_modifyPayType').hide().prev().show();
    }

    currAddress.prototype.setInfo = function(type, id, info, receiver, address, mobile, provT, cityT, distT)
    {
      var _this = this;
      if (type == 'new') {
        $(this.option.listDiv).find('li').removeClass('curr');
        $(this.option.listDiv).find('.radio-bg').removeClass('checked');
        $(this.option.listDiv).find('input').attr('checked', false);
        $(this.option.listDiv).prepend('<li class="curr js_addressEdit" data-address_id="' + id + '"><label><i class="radio-bg checked"></i><input type="radio js_sltAddress" value="'+id+'" class="radio1">' + info + '</label><span data-type="child" data-address_id="'+id+'">编辑</span></li>');
        $(this.option.parentDiv).attr('data-address_id', id);
        if (this.option.paydiv) {
          $(this.option.paydiv).addClass('hidden');
          $(this.option.sltPayDiv).removeClass('hidden');
        }
      } else {
        $(this.option.listDiv).find('li').each(function() {
          if ($(this).attr('data-address_id') == id) {
            $(this).html('<label><i class="radio-bg checked"></i><input type="radio js_sltAddress" value="'+id+'" class="radio1">' + info + '</label><span data-type="child" data-address_id="'+id+'">编辑</span>');
            _this.sltChange($(this));
          }
        });
        $(_this.option.parentDiv).attr('data-address_id', id);
      }

      $(_this.option.parentDiv).attr('data-edit-address', 0);
      getExpress();

      $('.js_modifyPayType').show().prev().hide();
    }

    new currAddress();

    $('.js_modifyAddress').on('click', function(){
      $('.js_payList').parent().addClass('hidden');
      $('.js_payInfo').removeClass('hidden');
      $('.js_orderUl').attr('data-edit-paytype', 0);
    });

    // 初始化快递
    getExpress();

    //删除地址
    $('body').on('click', '.js_deleteAddress',function(e){
      e.stopPropagation();
      var that = $(this);
      var id = $(this).data('address_id');
      var url = siteUrl+'/address/delete_address';

      if (btnClick() === false) {
        return false;
      }
      showConfirm('您确定要删除当前地址', function(){
        $.post(url, { id:id }, function(json){
          if (json.info == 'ok') {
            that.parent().remove();
          } else {
            showMsg(json.info, 'error');
          }
          btnClick('reset');
        }, 'json');
      },function(){})
    });

    // 修改配送方式
    $('.js_modifyPayType').on('click', function() {
      var editAddress = $('.js_orderUl').attr('data-edit-address');
      if (editAddress == 1) {
        return false;
      }
      //getExpress();

      $('.js_payList').parent().removeClass('hidden');
      $('.js_payInfo').addClass('hidden');
      $('.js_orderUl').attr('data-edit-paytype', 1);

      $('.js_modifyAddress').hide().prev().show();
    });

    // 保存
    $('.js_savePayType').on('click', function(){
      var payType      = $('.js_payList').attr('data-pay_type');
      var expressId    = $('.js_expressList').attr('data-express_id');
      var expressTitle = $('.js_expressList').attr('data-express_title');
      var addressId    = $('.js_orderUl').attr('data-address_id');
      var url          = siteUrl+'/express/get_express_price';

      if (expressId == '') {
        showMsg('请选择配送方式');
        return false;
      }
      $.get(url, { express_id:expressId, address_id:addressId }, function(json){
        if (json.info == 'ok') {
          $('.js_expressTitle').html(expressTitle);

          $('.js_expressTotalPrice').html(json.price+'.00元');
          $('.js_orderUl').attr('data-express_error', '');
          $('.js_orderUl').attr('data-express_price', json.price);
          $('.js_orderUl').attr('data-express_id', expressId);
          $('.js_orderCountPrice').html(json.order_price);
        } else {
          $('.js_orderUl').attr('data-express_error', json.info);
        }
      }, 'json');

      $('.js_orderUl').attr('data-pay_type', payType);
      $('.js_orderPayType').html($('.js_orderPayTypeInput[checked]').val()+'支付');

      $('.js_payList').parent().addClass('hidden');
      $('.js_payInfo').removeClass('hidden');
      $('.js_orderUl').attr('data-edit-paytype', 0);

      $('.js_modifyAddress').show().prev().hide();
    });

    $('.js_payList li').on('click', function(){
      var payType = $(this).attr('data-pay_type');
      $('.js_payList').attr('data-pay_type', payType);
      sltLI($(this));
    });
    $('.js_expressList li').on('click', function(){
      var expressId    = $(this).attr('data-express_id');
      var expressTitle = $(this).attr('data-express_title');
      var isAllow      = $(this).attr('data-is_allow');
      if (isAllow == 0) {
        return false;
      }
      $('.js_expressList').attr('data-express_id', expressId);
      $('.js_expressList').attr('data-express_title', expressTitle);
      sltLI($(this));
    });

    // 发票
    $('.js_invoiceList label').on('click', function(){
      var type = $(this).data('type');
      $('.js_orderUl').attr('data-invoice_type', type);
      $('.js_orderUl').attr('data-invoice_title', '');
      $('.js_invoiceInput').val('');
      if (type == 2) {
        $('.js_invoiceInput').removeClass('hidden');
      } else {
        $('.js_invoiceInput').addClass('hidden');
      }
    });
    $('.js_invoiceInput').on('keyup', function(){
      var invoiceTitle = $(this).val();
      $('.js_orderUl').attr('data-invoice_title', invoiceTitle);
    });

    // 优惠券使用
    $('.js_useCoupon').on('click', function(){
      //$('.js_couponInfo').find('input').removeClass('focus');
      $('.js_couponInfo').find('.prompt').html('');
      var couponCode = $('.js_orderUl').attr('data-coupon_code');
      if (couponCode) {
        $('.js_orderUl').attr('data-coupon_code', '');
        $('.js_orderCountPrice').html($('.js_orderCountPrice').attr('data-order_price'));
        $('.js_couponSubPrice').html('-0元');
        $('.js_useCoupon').removeClass('current');
        $('.js_useCoupon').find('.check-wrap').removeClass('checked');
      } else {
        if ($('.js_useCoupon').hasClass('current')) {
          $('.js_useCoupon').removeClass('current');
          $('.js_useCoupon').find('.check-wrap').removeClass('checked');
          $('.js_couponInfo').addClass('hidden');
        } else {
          $('.js_useCoupon').addClass('current');
          $('.js_useCoupon').find('.check-wrap').addClass('checked');
          $('.js_couponInfo').removeClass('hidden');
        }
      }

      return false;
    });

    $('.js_couponCancel').on('click', function(){
      $('.js_couponInfo').find('.prompt').html('');
      $('.js_couponInfo').find('input').val('');
      $('.js_orderUl').attr('data-coupon_code', '');
      $('.js_couponSubPrice').html('-0元');
      $('.js_orderCountPrice').html($('.js_orderCountPrice').attr('data-order_price'));
      $('.js_couponInfo').addClass('hidden');
      $('.js_useCoupon').removeClass('current');
      $('.js_useCoupon').find('.check-wrap').removeClass('checked');
    });

    $('.js_couponSave').on('click', function(){
      var couponCode = $('.js_couponInfo').find('input').val();
      var expressPrice = $('.js_orderUl').attr('data-express_price');
      if (!couponCode) {
        $('.js_couponInfo').find('.prompt').html('请输入优惠券编码');
      }
      
      //传入sku_id,num
      var sku_id = $('#js_orderForm input[name="sku_id"]').val();
      var num    = $('#js_orderForm input[name="num"]').val();
      var postdata = { coupon_code:couponCode, is_price:1, express_price:expressPrice };
      if(sku_id) {
        postdata.sku_id = sku_id;
        postdata.num    = num;
      }

      var url = siteUrl+'/coupon/check_coupon';
      $.post(url, postdata, function(json){
        if (json.info == 'ok') {
          $('.js_couponSubPrice').html('-'+json.price+'元');
          $('.js_orderCountPrice').html(json.order_price);
          $('.js_couponInfo').addClass('hidden');
          $('.js_orderUl').attr('data-coupon_code', couponCode);
        } else {
          $('.js_couponInfo').find('.prompt').html(json.info);
        }
      }, 'json');
    });
    /*
    data-invoice_type="" data-invoice_title="" data-edit-paytype="1" data-edit-address="0" data-address_id="3" data-pay_type=""
    */
    // 提交订单
    $('.js_subOrder').on('click', function(){
      var editAddress = $('.js_orderUl').attr('data-edit-address');
      var editPayType = $('.js_orderUl').attr('data-edit-paytype');
      var addressId   = $('.js_orderUl').attr('data-address_id');
      var payType     = $('.js_orderUl').attr('data-pay_type');
      var invoiceType = $('.js_orderUl').attr('data-invoice_type');
      var invoiceTitle = $('.js_orderUl').attr('data-invoice_title');
      var couponCode  = $('.js_orderUl').attr('data-coupon_code');
      
      /*
      var expressId   = $('.js_orderUl').attr('data-express_id');
      var expressError= $('.js_orderUl').attr('data-express_error');

      if (expressError) {
        showMsg(expressError);
        return;
      }*/
      if(invoiceType == 2) {
        if(invoiceTitle == '') {
          showMsg('请您先填写发票抬头再进行付款。');
          return false;
        }
      }
      if (editAddress > 0) {
        showMsg('请保存收获地址后，提交订单');
        return;
      }
      if (editPayType > 0) {
        showMsg('请先选择支付方式');
        return;
      }

      $('#js_orderForm').find('input[name=address]').val(addressId);
      $('#js_orderForm').find('input[name=invoice_type]').val(invoiceType);
      $('#js_orderForm').find('input[name=invoice_title]').val(invoiceTitle);
      $('#js_orderForm').find('input[name=coupon_code]').val(couponCode);
      $('#js_orderForm').find('input[name=pay_type]').val(payType);
      //$('#js_orderForm').find('input[name=express_id]').val(expressId);

      $('#js_orderForm').submit();

      $('.js_orderPayEnd').removeClass('hidden').prev().removeClass('hidden');
    });
  });

  function sltLI(obj)
  {
    obj.find('.radio1').attr("checked", 'checked');
    obj.siblings().find('.radio1').attr("checked",false);
    if(obj.find('.radio1').attr("checked")){
      obj.find('.radio-bg').addClass('checked').end().siblings().find('.radio-bg').removeClass('checked');
      obj.addClass('curr').siblings().removeClass('curr');
    }
  }

  function getExpress()
  {
    return false; // 取消用户选择快递
    var addressId = $('.js_orderUl').attr('data-address_id');
    var url = siteUrl+'/express/get_express_price';
    var suerExpressId = $('.js_expressList').attr('data-express_id');

    $('.js_expressList').find('.curr').removeClass('curr');
    $('.js_expressList').find('.checked').removeClass('checked');
    $('.js_expressList').find('li').attr('data-is_allow', 0);
    $('.js_expressList').attr('data-is_suer', 0);
    $('.js_expressList').attr('data-express_id', '');
    $('.js_expressList').attr('data-express_title', '');

    $('.js_expressList li').each(function(){
      var expressId = $(this).attr('data-express_id');
      var that = $(this);
      var title = $(this).attr('data-express_title');
      $(this).find('span').html('运费计算中，请稍等');
      $.get(url, { express_id:expressId, address_id:addressId }, function(json){
        if (json.info == 'ok') {
          that.attr('data-is_allow', 1);
          that.find('span').html('运费：'+json.price+'元');
          that.find('span').css({ color:'#000' });
          that.find('label').css({ color:'#000' });
          var isSure = $('.js_expressList').attr('data-is_suer');
          if ((!that.siblings().hasClass('curr') && isSure == 0) || expressId == suerExpressId) {
            $('.js_expressList').attr('data-has_error', 1);
            if (expressId == suerExpressId) { //如果相同 先固定
              $('.js_expressList').attr('data-is_suer', 1);
              $('.js_expressList').find('.curr').removeClass('curr');
              $('.js_expressList').find('.checked').removeClass('checked');
            }

            that.addClass('curr').find('i').addClass('checked');
            $('.js_expressList').attr('data-express_id', expressId);
            $('.js_expressList').attr('data-express_title', title);
            $('.js_expressTitle').html(title);

            $('.js_expressTotalPrice').html(json.price+'.00元');
            $('.js_orderUl').attr('data-express_price', json.price);
            $('.js_orderUl').attr('data-express_error', '');
            $('.js_orderUl').attr('data-express_id', expressId);
            $('.js_orderCountPrice').html(json.order_price);
          }
        } else {
          var hasError = $('.js_expressList').attr('data-has_error');
          if (!hasError) {
            $('.js_orderUl').attr('data-express_error', json.info);
          }
          that.find('span').html(json.info);
          that.find('span').css({ color:'#AAA' });
          that.find('label').css({ color:'#AAA' });
        }
      }, 'json');
    });
  }
})();